package com.ruoyi.mf178.service;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.mf178.domain.*;
import com.ruoyi.mf178.utils.Mf178Utils;
import com.ruoyi.mf178.vo.Mf178OrderStatus;
import com.ruoyi.mf178.vo.OrderStatus;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class Mf178Api {
    @Value("${mf178.baseUrl}")
    private String baseUrl;
    @Value("${mf178.appKey}")
    private String appKey;
    @Value("${mf178.appSecret}")
    private String appSecret;


    /**
     * 获取商品（全部）接口
     */
    public static final String GOODS_ALL = "/userapi/sgd/getSupplyGoodManageList";
    /**
     * 修改商品报价（已供应）接口
     */
    public static final String GOODS_EDIT = "/userapi/sgd/editSupplyGoodManageStock";

    public static final String UPDATE_STATUS = "/userapi/sgd/updateStatus";

    /**
     * 获取商品（全部）接口
     *
     * @param query
     * @return
     */
    public Mf178Response getSupplyGoodManageList(SupplyGoodManageQuery query) {
        String result = Mf178Utils.mf178DoPostJson(baseUrl + GOODS_ALL, appKey, appSecret, JSONObject.toJSONString(query));
        Mf178Response resultJson = JSONObject.parseObject(result, Mf178Response.class);
        return resultJson;
    }

    /**
     * 修改商品报价（已供应）接口
     *
     * @param editGoodsList
     * @return
     */
    public Mf178Response editSupplyGoodManageStock(List<EditGoods> editGoodsList) {
        Map<String, List<EditGoods>> map = new HashMap<>();
        map.put("goods", editGoodsList);
        String result = Mf178Utils.mf178DoPostJson(baseUrl + GOODS_EDIT, appKey, appSecret, JSONObject.toJSONString(map));
        return JSONObject.parseObject(result, Mf178Response.class);
    }

    public Mf178Response updateStatus(Mf178Order order) {
        Mf178OrderReportData mf178OrderReportData = new Mf178OrderReportData();
        mf178OrderReportData.setUser_order_id(order.getExternalOrderNo());
        if (order.getOrderStatus().equals(OrderStatus.Succeed)) {
            mf178OrderReportData.setStatus(9);
            mf178OrderReportData.setRsp_info(order.getOrderSummary());
            List<Mf178YsCard> ys_cards = new ArrayList();
            if (!StringUtils.isBlank(order.getCardNo())) {
                String[] cardNos = order.getCardNo().split(",");
                for (int i = 0; i < cardNos.length; i++) {
                    Mf178YsCard mf178YsCard = new Mf178YsCard();
                    if (order.getCardType() != null) {
                        mf178YsCard.setCard_type(Integer.parseInt(order.getCardType()));// 可能异常，cardType如果不为int的话
                    }
                    mf178YsCard.setCard_no(cardNos[i]);
                    ys_cards.add(mf178YsCard);
                }
            }
            if (!StringUtils.isBlank(order.getCardPwd())) {
                String[] cardPwds = order.getCardPwd().split(",");
                for (int i = 0; i < cardPwds.length; i++) {
                    Mf178YsCard mf178YsCard = new Mf178YsCard();
                    if (order.getCardType() != null) {
                        mf178YsCard.setCard_type(Integer.parseInt(order.getCardType()));// 可能异常，cardType如果不为int的话
                    }
                    mf178YsCard.setCard_pwd(cardPwds[i]);
                    ys_cards.add(mf178YsCard);
                }
            }

            mf178OrderReportData.setYs_cards(ys_cards);
        } else if (order.getOrderStatus().equals(OrderStatus.Failed)) {
            mf178OrderReportData.setStatus(8);
        } else {
            mf178OrderReportData.setStatus(7);
        }
        String result = Mf178Utils.mf178DoPostJson(baseUrl + UPDATE_STATUS, appKey, appSecret, JSONObject.toJSONString(mf178OrderReportData));
        return JSONObject.parseObject(result, Mf178Response.class);
    }

}
